﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HenIT.Data.SqlClient;
using System.Data.SqlClient;
using System.Data;
namespace EventScavenger
{
    public partial class EventScavengerDAL : GenericSQLServerDAL
    {
        internal void DeletePollFilter(PollFilter pollFilter)
        {
            string sql = "DeletePollFilter";
            SqlParameter[] parameters = new SqlParameter[] 
				{
					new SqlParameter("@PollFilterID", pollFilter.PollFilterID)
				};
            ExecuteSP(sql, parameters);
        }
        internal List<PollFilter> GetPollingFilterList()
        {
            List<PollFilter> filters = new List<PollFilter>();
            string sql = "SelectPollingFilters";
            DataSet pfds = GetDataSet(sql, CommandType.StoredProcedure);
            if (pfds.Tables.Count > 0 && pfds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow r in pfds.Tables[0].Rows)
                {
                    PollFilter pollFilter = new PollFilter()
                    {
                        PollFilterID = (int)r["PollFilterID"],
                        Name = r["Name"].ToString(),
                        MessageContains = (r["MessageContains"] == DBNull.Value ? "" : r["MessageContains"].ToString()),
                        ExcludeInfo = (bool)r["ExcludeInfo"],
                        ExcludeWarn = (bool)r["ExcludeWarn"],
                        ExcludeErr = (bool)r["ExcludeErr"],
                        ExcludeSucAud = (bool)r["ExcludeSucAud"],
                        ExcludeFailAud = (bool)r["ExcludeFailAud"],
                        LastChanged = (r["LastChanged"] == DBNull.Value ? null : (DateTime?)r["LastChanged"])
                    };
                    if ((int)r["EventIDFilterCount"] > 0)
                    {
                        sql = "SelectPollFilterEventIDsByPollFilterID";
                        SqlParameter[] parameters2 = new SqlParameter[] 
						{
							new SqlParameter("@PollFilterID", pollFilter.PollFilterID),
						};
                        DataSet pfeidds = GetDataSet(sql, CommandType.StoredProcedure, parameters2);
                        if (pfeidds.Tables.Count > 0 && pfeidds.Tables[0].Rows.Count > 0)
                        {
                            foreach (DataRow rid in pfeidds.Tables[0].Rows)
                            {
                                if ((bool)rid["Exclude"])
                                {
                                    pollFilter.ExcludeEventIDs.Add((int)rid["EventID"]);
                                }
                                else
                                {
                                    pollFilter.IncludeEventIDs.Add((int)rid["EventID"]);
                                }
                            }
                        }
                    }
                    if ((int)r["SourceFilterCount"] > 0)
                    {
                        sql = "SelectPollFilterSourcesByPollFilterID";
                        SqlParameter[] parameters3 = new SqlParameter[] 
						{
							new SqlParameter("@PollFilterID", pollFilter.PollFilterID),
						};
                        DataSet pfsds = GetDataSet(sql, CommandType.StoredProcedure, parameters3);
                        if (pfsds.Tables.Count > 0 && pfsds.Tables[0].Rows.Count > 0)
                        {
                            foreach (DataRow rs in pfsds.Tables[0].Rows)
                            {
                                if ((bool)rs["Exclude"])
                                {
                                    pollFilter.ExcludeSources.Add(rs["Source"].ToString());
                                    pollFilter.ExcludeSourceIDs.Add((int)rs["SourceID"]);
                                }
                                else
                                {
                                    pollFilter.IncludeSources.Add(rs["Source"].ToString());
                                    pollFilter.IncludeSourceIDs.Add((int)rs["SourceID"]);
                                }
                            }
                        }
                    }
                    filters.Add(pollFilter);
                }

            }
            return filters;
        }
        internal void InsertPollFilter(PollFilter pollFilter)
        {
            string sql = "InsertPollFilter";
            StringBuilder pollFilterEventIDIncludes = new StringBuilder();
            StringBuilder pollFilterEventIDExcludes = new StringBuilder();
            StringBuilder pollFilterSourceIncludes = new StringBuilder();
            StringBuilder pollFilterSourceExcludes = new StringBuilder();
            if (pollFilter.IncludeEventIDs.Count() > 0)
            {
                foreach (int i in pollFilter.IncludeEventIDs)
                {
                    pollFilterEventIDIncludes.Append(i.ToString() + ",");
                }
            }
            if (pollFilter.ExcludeEventIDs.Count() > 0)
            {
                foreach (int i in pollFilter.ExcludeEventIDs)
                {
                    pollFilterEventIDExcludes.Append(i.ToString() + ",");
                }
            }
            if (pollFilter.IncludeSourceIDs.Count() > 0)
            {
                foreach (int i in pollFilter.IncludeSourceIDs)
                {
                    pollFilterSourceIncludes.Append(i.ToString() + ",");
                }
            }
            if (pollFilter.ExcludeSourceIDs.Count() > 0)
            {
                foreach (int i in pollFilter.ExcludeSourceIDs)
                {
                    pollFilterSourceExcludes.Append(i.ToString() + ",");
                }
            }

            SqlParameter[] parameters = new SqlParameter[] 
				{
					new SqlParameter("@Name", pollFilter.Name),
					new SqlParameter("@MessageContains", pollFilter.MessageContains),
					new SqlParameter("@ExcludeInfo", pollFilter.ExcludeInfo),
					new SqlParameter("@ExcludeWarn", pollFilter.ExcludeWarn),
					new SqlParameter("@ExcludeErr", pollFilter.ExcludeErr),
					new SqlParameter("@ExcludeSucAud", pollFilter.ExcludeSucAud),
					new SqlParameter("@ExcludeFailAud", pollFilter.ExcludeFailAud),
					new SqlParameter("@PollFilterEventIDIncludes", pollFilterEventIDIncludes.ToString().TrimEnd(',')),
					new SqlParameter("@PollFilterEventIDExcludes", pollFilterEventIDExcludes.ToString().TrimEnd(',')),
					new SqlParameter("@PollFilterSourceIncludes", pollFilterSourceIncludes.ToString().TrimEnd(',')),
					new SqlParameter("@PollFilterSourceExcludes", pollFilterSourceExcludes.ToString().TrimEnd(','))
				};
            ExecuteSP(sql, parameters);
        }
        internal void UpdatePollFilter(PollFilter pollFilter)
        {
            string sql = "UpdatePollFilter";
            StringBuilder pollFilterEventIDIncludes = new StringBuilder();
            StringBuilder pollFilterEventIDExcludes = new StringBuilder();
            StringBuilder pollFilterSourceIncludes = new StringBuilder();
            StringBuilder pollFilterSourceExcludes = new StringBuilder();
            if (pollFilter.IncludeEventIDs.Count() > 0)
            {
                foreach (int i in pollFilter.IncludeEventIDs)
                {
                    pollFilterEventIDIncludes.Append(i.ToString() + ",");
                }
            }
            if (pollFilter.ExcludeEventIDs.Count() > 0)
            {
                foreach (int i in pollFilter.ExcludeEventIDs)
                {
                    pollFilterEventIDExcludes.Append(i.ToString() + ",");
                }
            }
            if (pollFilter.IncludeSourceIDs.Count() > 0)
            {
                foreach (int i in pollFilter.IncludeSourceIDs)
                {
                    pollFilterSourceIncludes.Append(i.ToString() + ",");
                }
            }
            if (pollFilter.ExcludeSourceIDs.Count() > 0)
            {
                foreach (int i in pollFilter.ExcludeSourceIDs)
                {
                    pollFilterSourceExcludes.Append(i.ToString() + ",");
                }
            }

            SqlParameter[] parameters = new SqlParameter[] 
				{
					new SqlParameter("@PollFilterID", pollFilter.PollFilterID),
					new SqlParameter("@Name", pollFilter.Name),
					new SqlParameter("@MessageContains", pollFilter.MessageContains),
					new SqlParameter("@ExcludeInfo", pollFilter.ExcludeInfo),
					new SqlParameter("@ExcludeWarn", pollFilter.ExcludeWarn),
					new SqlParameter("@ExcludeErr", pollFilter.ExcludeErr),
					new SqlParameter("@ExcludeSucAud", pollFilter.ExcludeSucAud),
					new SqlParameter("@ExcludeFailAud", pollFilter.ExcludeFailAud),
					new SqlParameter("@PollFilterEventIDIncludes", pollFilterEventIDIncludes.ToString().TrimEnd(',')),
					new SqlParameter("@PollFilterEventIDExcludes", pollFilterEventIDExcludes.ToString().TrimEnd(',')),
					new SqlParameter("@PollFilterSourceIncludes", pollFilterSourceIncludes.ToString().TrimEnd(',')),
					new SqlParameter("@PollFilterSourceExcludes", pollFilterSourceExcludes.ToString().TrimEnd(','))
				};
            ExecuteSP(sql, parameters);
        }
    }
}
